<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/13 0013
 * Time: 10:10
 */
namespace app\printer\controller;

use think\Controller;
use think\Db;
class LoginController extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    //登录请求
    public function postLogin()
    {

        $postData = $this->request->post();
        $username = $postData['username'];
        $password = $postData['password'];
        $map = array();
        $map['user_name'] = array('eq', $username);
        $map['pass_word'] = array('eq', $password);

        $user_info = Db::table('user_info')->where($map)->find();
        if(!empty($user_info) and $username == $user_info['user_name'] and $password == $user_info['pass_word']){
            session('user_name', $user_info['user_name']);
            session('user_id', $user_info['id']);
            Db::table('user_info')->where(array('id'=>$user_info['id']))->data(array('last_login_time'=> date('Y-m-d H:i:s')))->update();
            return ['code' => 1];
        }else{
            return ['code' => 0];
        }
    }

    //退出
    public function logout()
    {
        session(null);
        $this->redirect(url('finance/Login/index'));
    }
}